import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:kxy_flutter_app/components/separator.dart';
import 'package:kxy_flutter_app/model/exam_result.dart';
import 'package:kxy_flutter_app/view/examination/exam_result/exam_result_style_widget.dart';

class ExamReusltHeaderWidget extends StatelessWidget {
  final ExamResult result;
  const ExamReusltHeaderWidget({Key key, this.result}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ExamResultStyleWidget(
      child: Container(
        margin: EdgeInsets.only(top: 20),
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            scoreWidget,
            titleWidget,
            timeWidget,
            Padding(
              padding: EdgeInsets.only(top: 18),
              child: Separator(
                color: Color.fromRGBO(240, 240, 240, 1),
                height: 2,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget get statusWidget {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      decoration: BoxDecoration(
        color: Color.fromRGBO(254, 181, 77, 1),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(15),
          topRight: Radius.circular(10),
          bottomLeft: Radius.circular(1),
          bottomRight: Radius.circular(15),
        ),
      ),
      child: Text(
        result.isPass ? '您及格了' : '您未及格',
        style: TextStyle(
          color: Colors.white,
        ),
      ),
    );
  }

  Widget get scoreWidget {
    var fenStyle = TextStyle(
      fontSize: 16,
      color: result.isPass ? Color.fromRGBO(83, 191, 140, 1) : Color.fromRGBO(255, 120, 102, 1),
    );
    var integralStyle = TextStyle(fontSize: 12, color: Color.fromRGBO(136, 136, 136, 1));
    var integralValStyle = TextStyle(fontSize: 12, color: Color.fromRGBO(254, 181, 77, 1));
    return Container(
      height: 70,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Container(
            alignment: Alignment.bottomCenter,
            child: Text(
              result.sumScore.toString(),
              style: TextStyle(
                fontSize: 60,
                color: result.isPass ? Color.fromRGBO(83, 191, 140, 1) : Color.fromRGBO(255, 120, 102, 1),
              ),
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              statusWidget,
              Row(
                children: [
                  Text('分', style: fenStyle),
                  Padding(
                    padding: EdgeInsets.only(left: 10),
                    child: Text('获得积分', style: integralStyle),
                  ),
                  Text('+${result.grade}', style: integralValStyle),
                ],
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget get titleWidget {
    return Container(
      height: 40,
      margin: EdgeInsets.symmetric(vertical: 10),
      padding: EdgeInsets.only(top: 10),
      child: Text(
        result.examName,
        style: TextStyle(
          fontSize: 24,
          color: Colors.black,
        ),
      ),
    );
  }

  Widget get timeWidget {
    var format = new DateFormat('yyyy-MM-dd HH:mm:ss');
    var infoColor = Color.fromRGBO(136, 136, 136, 1);

    String text;
    if (result.duration < 0) {
      text = '不限时长考试';
    } else {
      int minutes = (result.duration / 60).floor();
      int seconds = result.duration % 60;
      text = '$minutes分$seconds秒';
    }
    return Container(
      height: 20,
      alignment: Alignment.bottomLeft,
      child: Row(
        children: [
          Padding(
            padding: EdgeInsets.only(right: 2),
            child: Icon(Icons.timer, size: 16, color: infoColor),
          ),
          Text(
            text,
            style: TextStyle(
              fontSize: 12,
              color: infoColor,
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(10, 0, 2, 0),
            child: Icon(Icons.check_circle_outline, size: 16, color: infoColor),
          ),
          Text(
            format.format(result.submitPaperTime),
            style: TextStyle(
              fontSize: 12,
              color: infoColor,
            ),
          )
        ],
      ),
    );
  }
}
